package cn.getech.data.metadata.service.impl;

import cn.getech.data.development.constant.PermissionTypeEnum;
import cn.getech.data.development.utils.DataPermissionUtil;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.NameValue;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.metadata.entity.meta.CollectionTableField;
import cn.getech.data.metadata.mapper.CollectionTableField2Mapper;
import cn.getech.data.metadata.model.req.collectiontablefield.CollectiionTableFieldListReq;
import cn.getech.data.metadata.model.res.CollectionTableFieldRes;
import cn.getech.data.metadata.service.CollectionTableField2Service;
import cn.getech.system.center.utils.ShiroUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CollectionTableField2ServiceImpl extends ServiceImpl<CollectionTableField2Mapper, CollectionTableField> implements CollectionTableField2Service {

    @Override
    public PageUtils<CollectionTableFieldRes> listBy(CollectiionTableFieldListReq req) {
        Map<String, Object> params = new HashMap<>();
        params.put("tableId", req.getTableId());
        if (req.getLimit() != null && req.getPage() != null) {
            params.put("offset", (req.getPage() - 1) * req.getLimit());
            params.put("limit", req.getLimit());
        }
        List<CollectionTableFieldRes> list = baseMapper.listBy(params);
        Integer count = baseMapper.listByCount(params);
        PageUtils<CollectionTableFieldRes> pageUtils = new PageUtils(list, count, req.getLimit(), req.getPage());
        return pageUtils;
    }

    // 判断用户定义元数据表是否拥有编辑权限
    @Override
    public  Map<String,Integer> metaEditPermission(Map<String, Integer> req) {
        if(StringUtils.isEmpty(req.get("user_id").toString())){
            throw new RRException("请输入useId");
        }
        if(StringUtils.isEmpty(req.get("table_id").toString())){
            throw new RRException("请输入TableId");
        }
        Map<String, Integer> params = new HashMap<>();
       /* Map<String, Integer> params = new HashMap<>();
        if(baseMapper.metaEditPermission(req) != null){
            if(baseMapper.metaEditPermission(req).equals(1)){
                // 1:拥有编辑权限
                params.put("EditPression", 1);
            }else{
                // 2:没有编辑权限
                params.put("EditPression", 0);
            }
        }else{
            // 2:没有编辑权限
            params.put("EditPression", 0);
        }*/
        String tableId= req.get("table_id").toString();
        NameValue nameValue= getEditAuth(Integer.parseInt(tableId));
        params.put("EditPression",Integer.parseInt(nameValue.getValue().toString()));
        return  params;
    }

    /**
    * @Description 获取编辑权限
    * @Author  chengweiping
    * @Date   2021/3/26 10:46
    */
    private NameValue getEditAuth(Integer tableId){
        NameValue nameValue=new NameValue();
        try{
            nameValue.setName("EditPression");
            nameValue.setValue(1);
            Long userId = ShiroUtils.getUserId();
            if (!DataPermissionUtil.isAdmin(userId.intValue())) {
                List<Integer> ids = DataPermissionUtil.getDataIds(  userId.intValue(),
                        PermissionTypeEnum.METADATA_MANAGEMENT.getType(),
                        PermissionTypeEnum.UPDATE.getType());
                if (CollectionUtils.isEmpty(ids)) {
                    ids.add(-1);
                }

                if(ids.contains(tableId)){
                    nameValue.setValue(1);
                }else{
                    nameValue.setValue(0);
                }
            }
        }catch ( Exception e){
            e.printStackTrace();
        }

        return nameValue;
    }
}
